﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#if !NETFX_CORE
using System.Drawing;
#endif

namespace Cofe.Core.Utils
{
#if !NETFX_CORE
    /// <summary>
    /// Bitmap related utils.
    /// </summary>
    public static class BitmapUtils
    {
        /// <summary>
        /// Make sure a iconBitmap has a specific size, if not, resize it.
        /// </summary>
        /// <param name="image"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static Bitmap EnsureBitmapSize(Bitmap image, Size size)
        {
            if (image.Width == size.Width && image.Height == size.Height)
                return image;

            if (size.Width == -1 && size.Height == -1)
                return image;

            var height = size.Height != -1 ? size.Height : image.Height * image.Width / size.Width;
            var width = size.Width != -1 ? size.Width : image.Width * image.Height / size.Height;

            if (size.Width <= 0 || size.Height <= 0)
                return image;

            return ResizeImage(image, size, 0);
        }

        //http://blog.paranoidferret.com/?p=11 , modified a little.
        /// <summary>
        /// Resize a Bitmap.
        /// </summary>
        /// <param name="imgToResize"></param>
        /// <param name="size"></param>
        /// <param name="spacing"></param>
        /// <returns></returns>
        public static Bitmap ResizeImage(Bitmap imgToResize, System.Drawing.Size size, int spacing)
        {
            lock (imgToResize)
            {
                if (imgToResize == null) return null;
                if (imgToResize.Width == size.Width && imgToResize.Height == size.Height && spacing == 0)
                    return imgToResize;

                int sourceWidth = imgToResize.Width;
                int sourceHeight = imgToResize.Height;

                if ((sourceWidth == size.Width) && (sourceHeight == size.Height))
                    return imgToResize;


                float nPercent = 0;
                float nPercentW = 0;
                float nPercentH = 0;

                nPercentW = ((float)size.Width / (float)sourceWidth);
                nPercentH = ((float)size.Height / (float)sourceHeight);

                if (nPercentH < nPercentW)
                    nPercent = nPercentH;
                else
                    nPercent = nPercentW;

                int destWidth = (int)((sourceWidth * nPercent) - spacing * 4);
                int destHeight = (int)((sourceHeight * nPercent) - spacing * 4);

                int leftOffset = (int)((size.Width - destWidth) / 2);
                int topOffset = (int)((size.Height - destHeight) / 2);


                Bitmap b = new Bitmap(size.Width, size.Height);
                using (Graphics g = Graphics.FromImage((Image)b))
                {
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                    if (spacing > 0)
                    {
                        g.DrawLines(System.Drawing.Pens.Silver, new PointF[] {
                        new PointF(leftOffset - spacing, topOffset + destHeight + spacing), //BottomLeft
                        new PointF(leftOffset - spacing, topOffset -spacing),                 //TopLeft
                        new PointF(leftOffset + destWidth + spacing, topOffset - spacing)});//TopRight

                        g.DrawLines(System.Drawing.Pens.Gray, new PointF[] {
                        new PointF(leftOffset + destWidth + spacing, topOffset - spacing),  //TopRight
                        new PointF(leftOffset + destWidth + spacing, topOffset + destHeight + spacing), //BottomRight
                        new PointF(leftOffset - spacing, topOffset + destHeight + spacing),}); //BottomLeft
                    }

                    g.DrawImage(imgToResize, leftOffset, topOffset, destWidth, destHeight);
                    //g.Dispose();
                }
                return b;

            }
        }

        /// <summary>
        /// Resize a iconBitmap and draw a frame on it.
        /// </summary>
        /// <param name="imgToResize"></param>
        /// <param name="size"></param>
        /// <param name="spacing"></param>
        /// <returns></returns>
        public static Bitmap ResizeJumbo(Bitmap imgToResize, System.Drawing.Size size, int spacing)
        {
            lock (imgToResize)
            {
                if (imgToResize.Width == size.Width && imgToResize.Height == size.Height && spacing == 0)
                    return imgToResize;

                if (imgToResize == null) return null;

                int sourceWidth = imgToResize.Width;
                int sourceHeight = imgToResize.Height;

                float nPercent = 0;
                float nPercentW = 0;
                float nPercentH = 0;

                nPercentW = ((float)size.Width / (float)sourceWidth);
                nPercentH = ((float)size.Height / (float)sourceHeight);

                if (nPercentH < nPercentW)
                    nPercent = nPercentH;
                else
                    nPercent = nPercentW;

                int destWidth = 80;
                int destHeight = 80;

                int leftOffset = (int)((size.Width - destWidth) / 2);
                int topOffset = (int)((size.Height - destHeight) / 2);


                Bitmap b = new Bitmap(size.Width, size.Height);
                using (Graphics g = Graphics.FromImage((Image)b))
                {
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                    g.DrawLines(System.Drawing.Pens.Silver, new PointF[] {
                        new PointF(0 + spacing, size.Height - spacing), //BottomLeft
                        new PointF(0 + spacing, 0 + spacing),                 //TopLeft
                        new PointF(size.Width - spacing, 0 + spacing)});//TopRight

                    g.DrawLines(System.Drawing.Pens.Gray, new PointF[] {
                        new PointF(size.Width - spacing, 0 + spacing),  //TopRight
                        new PointF(size.Width - spacing, size.Height - spacing), //BottomRight
                        new PointF(0 + spacing, size.Height - spacing)}); //BottomLeft

                    g.DrawImage(imgToResize, leftOffset, topOffset, destWidth, destHeight);
                    //   g.Dispose();
                }

                return b;
            }
        }
    }
#endif
}
